<template>
  <div>
    <el-form :inline="true" :model="post" class="demo-form-inline">
      <el-form-item label="postName">
        <el-input v-model="post.postName" placeholder="postName"></el-input>
      </el-form-item>
      <el-form-item label="postCode">
        <el-input v-model="post.postCode" placeholder="postCode"></el-input>
      </el-form-item>
      <el-form-item label="status">
        <el-select v-model="post.status" placeholder="status">
          <el-option label="正常" :value="0"></el-option>
          <el-option label="停用" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="list">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
        :data="tableData"
        border
        style="width: 100%"
        tooltip-effect="dark"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          fixed
          prop="id"
          label="id"
          width="150">
      </el-table-column>
      <el-table-column
          prop="postName"
          label="postName"
          width="120">
      </el-table-column>
      <el-table-column
          prop="postCode"
          label="postCode"
          width="120">
      </el-table-column>
      <el-table-column
          prop="sequence"
          label="sequence"
          width="120">
      </el-table-column>
      <el-table-column
          prop="status"
          label="status"
          width="120">
        <template v-slot="s">
          <el-tag v-if="s.row.status==0">正常</el-tag>
          <el-tag v-if="s.row.status==1">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="remark"
          label="remark"
          width="120">
      </el-table-column>
      <el-table-column
          prop="updateTime"
          label="updateTime"
          width="120">
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="100">
        <template slot-scope="scope">
          <el-button @click="delById(scope.row.id)" type="text" size="small">删除</el-button>
          <el-button @click="openUpdate(scope.row)" type="text" size="small">修改</el-button>
          <el-button type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @current-change="goPage"
        :page-size="size"
        layout="total, prev, pager, next"
        :total="total">
    </el-pagination>

    <el-dialog title="添加岗位" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="postName" :label-width="formLabelWidth">
          <el-input v-model="form.postName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="postCode" :label-width="formLabelWidth">
          <el-input v-model="form.postCode" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="sequence" :label-width="formLabelWidth">
          <el-input v-model="form.sequence" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="status" :label-width="formLabelWidth">
          <el-select v-model="form.status" placeholder="status">
            <el-option label="正常" :value="0"></el-option>
            <el-option label="停用" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="remark" :label-width="formLabelWidth">
          <el-input v-model="form.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="postAdd()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改岗位" :visible.sync="dialogFormVisible1">
      <el-form :model="form1">
        <el-form-item label="postName" :label-width="formLabelWidth1">
          <el-input v-model="form1.postName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="postCode" :label-width="formLabelWidth">
          <el-input v-model="form1.postCode" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="sequence" :label-width="formLabelWidth">
          <el-input v-model="form1.sequence" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="status" :label-width="formLabelWidth">
          <el-select v-model="form1.status" placeholder="status">
            <el-option label="正常" :value="0"></el-option>
            <el-option label="停用" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="remark" :label-width="formLabelWidth">
          <el-input v-model="form1.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateById()">确 定 修 改</el-button>
      </div>
    </el-dialog>

    <el-button @click="openAdd">添加</el-button>
    <el-button @click="delAll">批量删除</el-button>
    <el-button @click="excel">excel导出</el-button>
  </div>
</template>

<script>
export default {
  name: "PostView",
  data() {
    return {
      tableData:[],
      size:3,
      total:0,
      current:1,
      post:{},
      form:{},
      dialogFormVisible:false,
      dialogFormVisible1:false,
      formLabelWidth:'120px',
      formLabelWidth1:'120px',
      form1:{},
      multipleSelection:[],
      ids:[]
    }
  }, methods: {
    excel(){
      window.location.href = "http://localhost:8003/post/excel";
    },
    delAll(){
      this.axios.post("/post/delAll?ids="+this.ids).then(()=>{
        this.$message.success("删除成功");
        this.list();
      })
    },
    handleSelectionChange(rows){
      console.log(rows);
      this.ids = rows.map(item=>item.id).join(",");
      console.log(this.ids)
    },
    updateById(){
      this.axios.post("post/updateById",this.form1).then(()=>{
        this.$message.success("修改成功");
        this.list();
        this.dialogFormVisible1 = false;
      })
    },
    openUpdate(row){
      this.dialogFormVisible1 = true;
      this.form1 = row;
    },
    delById(id){
      this.axios.post("/post/delById?id="+id).then(()=>{
        this.$message.success("删除成功");
        this.list();
      })
    },
    postAdd(){
      this.axios.post("/post/postAdd",this.form).then(()=>{
        this.$message.success("添加成功");
        this.list();
        this.dialogFormVisible = true;
      })
    },
    openAdd(){
      this.dialogFormVisible = true;
    },
    goPage(page){
      this.current = page;
      this.list();
    },
    list(){
      this.axios.post("/post/postList?current="+this.current+"&size="+this.size,this.post).then(res=>{
        this.tableData = res.data.data.list;
        this.total = res.data.data.total;
      })
    }
  }, created() {
    this.list();
  }
}
</script>

<style scoped>

</style>
